Code:- #include <stdio.h>
- typedef struct {
- char nombre[30];
- int codigo;
- float costo;
- float precio;
- int cantidad;
- } producto;
- void entrarregistro(producto registro);
- void modificarcodigo(producto registro);
- void modificarnombre(producto registro);
- void modificarcosto(producto registro);
- void modificarprecio(producto registro);
- void modificarcantidad(producto registro);
- void buscarproducto(producto registro);
- int main(int argc, const char * argv[])
- {
- producto(registro);
- int centinela;
- do {
- printf("Menu:\n");
- printf("0.- Salir del programa\n");
- printf("1.- Entrar nuevo registro\n");
- printf("2.- Modificar codigo de producto\n");
- printf("3.- Modificar nombre de producto\n");
- printf("4.- Modificar costo de producto\n");
- printf("5.- Modificar precio de producto\n");
- printf("6.- Modificar cantidad de producto\n");
- printf("7.- Buscar un producto\n");
- printf("Seleccione una opcion:\n");
- scanf("%d", ¢inela);
- switch(centinela){
- case 0:
- break;
- case 1:
- entrarregistro(registro);
- break;
- case 2:
- modificarcodigo(registro);
- break;
- case 3:
- modificarnombre(registro);
- break;
- case 4:
- modificarcosto(registro);
- break;
- case 5:
- modificarprecio(registro);
- break;
- case 6:
- modificarcantidad(registro);
- break;
- case 7:
- buscarproducto(registro);
- break;
- }
- } while (centinela != 0);
- return 0;
- }
- void entrarregistro(producto registro)
- {
- FILE * fd;
- fd = fopen("/Users/David/Desktop/datos.dat", "a");
- if(fd == NULL){
- printf("Error al abrir el archivo");
- exit(1);
- }
- printf("Entre el nombre del producto:\n");
- scanf("%s", registro.nombre);
- printf("Entre el codigo del producto:\n");
- scanf("%d", ®istro.codigo);
- printf("Entre el costo del producto:\n");
- scanf("%f", ®istro.costo);
- printf("Entre el precio del producto:\n");
- scanf("%f", ®istro.precio);
- printf("Entre la cantidad:\n");
- scanf("%d", ®istro.cantidad);
- fprintf(fd, "%s %d %7.2f %7.2f %d\n", registro.nombre, registro.codigo, registro.costo, registro.precio,registro.cantidad);
- fclose(fd);
- }
- void modificarcodigo(producto registro)
- {
- FILE * fd;
- int c = 0;
- fd = fopen("/Users/David/Desktop/datos.dat", "rb+");
- if(fd == NULL){
- printf("Error al abrir el archivo");
- exit(1);
- }
- printf("Seleccina el codigo del producto a modificar: \n");
- fread(®istro, sizeof(producto), 1, fd);
- scanf("%d", &c);
- fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
- fread(®istro, sizeof(producto), 1, fd);
- printf("Entre el nuevo codigo:\n");
- scanf("%d", ®istro.codigo);
- fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
- fwrite(®istro, sizeof(producto), 1, fd);
- fclose(fd);
- }
- void modificarnombre(producto registro)
- {
- FILE * fd;
- int c = 0;
- fd = fopen("/Users/David/Desktop/datos.dat", "rb+");
- if(fd == NULL){
- printf("Error al abrir el archivo");
- exit(1);
- }
- printf("Seleccina el codigo del producto a modificar: \n");
- fread(®istro, sizeof(producto), 1, fd);
- while(!feof(fd)){
- fread(®istro, sizeof(producto), 1, fd);
- }
- printf("?:\n");
- scanf("%d", &c);
- fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
- fread(®istro, sizeof(producto), 1, fd);
- printf("Entre el nuevo nombre : \n");
- scanf("%s", registro.nombre);
- fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
- fwrite(®istro, sizeof(producto), 1, fd);
- fclose(fd);
- }
- void modificarcosto(producto registro)
- {
- FILE * fd;
- int c = 0;
- fd = fopen("/Users/David/Desktop/datos.dat", "rb+");
- if(fd == NULL){
- printf("Error al abrir el archivo");
- exit(1);
- }
- printf("Seleccina el codigo del producto a modificar: \n");
- fread(®istro, sizeof(producto), 1, fd);
- while(!feof(fd)){
- fread(®istro, sizeof(producto), 1, fd);
- }
- printf("?:\n");
- scanf("%d", &c);
- fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
- fread(®istro, sizeof(producto), 1, fd);
- printf("Entre el nuevo costo: \n");
- scanf("%f", ®istro.costo);
- if(registro.costo < 0){
- printf("No se puede tener un costo por debajo de 0");
- exit(1);
- }
- fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
- fwrite(®istro, sizeof(producto), 1, fd);
- fclose(fd);
- }
- void modificarprecio(producto registro)
- {
- FILE * fd;
- int c = 0;
- fd = fopen("/Users/David/Desktop/datos.dat", "rb+");
- if(fd == NULL){
- printf("Error al abrir el archivo");
- exit(1);
- }
- printf("Seleccina el codigo del producto a modificar: \n");
- fread(®istro, sizeof(producto), 1, fd);
- while(!feof(fd)){
- fread(®istro, sizeof(producto), 1, fd);
- }
- printf("?:\n");
- scanf("%d", &c);
- fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
- fread(®istro, sizeof(producto), 1, fd);
- printf("Entre el nuevo numero precio: \n");
- scanf("%f", ®istro.precio);
- if(registro.precio < 0){
- printf("No se puede tener un precio por debajo de 0");
- exit(1);
- }
- else{
- fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
- fwrite(®istro, sizeof(producto), 1, fd);
- fclose(fd);
- }
- }
- void modificarcantidad(producto registro)
- {
- FILE * fd;
- int c = 0;
- fd = fopen("/Users/David/Desktop/datos.dat", "rb+");
- if(fd == NULL){
- printf("Error al abrir el archivo");
- exit(1);
- }
- printf("Seleccina el codigo del producto a modificar: \n");
- fread(®istro, sizeof(producto), 1, fd);
- while(!feof(fd)){
- fread(®istro, sizeof(producto), 1, fd);
- }
- printf("?:\n");
- scanf("%d", &c);
- fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
- fread(®istro, sizeof(producto), 1, fd);
- printf("Entre la cantidad nueva: \n");
- scanf("%d", ®istro.cantidad);
- if(registro.cantidad < 0){
- printf("No se pueden tener cantidades negativas\n");
- exit(1);
- }
- else{
- fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
- fwrite(®istro, sizeof(producto), 1, fd);
- fclose(fd);
- }
- }
- void buscarproducto(producto registro)
- {
- FILE * fd;
- int c;
- fd = fopen("/Users/David/Desktop/datos.dat", "r");
- if (fd == NULL){
- printf("Error al abrir el archivo");
- exit(1);
- }
- printf("Entre el codigo del producto a buscar:\n");
- fread(®istro, sizeof(producto), 1, fd);
- while (!feof(fd)){
- fread(®istro, sizeof(producto), 1, fd);
- }
- scanf("%d", &c);
- fseek(fd, sizeof(producto) * (c-1), SEEK_SET);
- fread(®istro, sizeof(producto), 1, fd);
- printf("%s\t%d\t%f\t%f\t%d\n", registro.nombre, registro.codigo, registro.costo, registro.precio,registro.cantidad);
- fclose(fd);
- }
So there is my whole code, it's a program to manage an inventory. The first function is to create a new entry, the last one is to search for a product using the code and the rest are to modify data. Anyway, now I need to know how to export all the data to a spreadsheet. If I open the file the program creates with excel it actually shows the name but after that it shows random symbols. Thanks for the help!